package com.hengxinguotong.zhihuichengjian.ui.security;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.SafetyTreeFirstAdapter;
import com.hengxinguotong.zhihuichengjian.bean.SafetyBean;
import com.hengxinguotong.zhihuichengjian.bean.SafetyRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.QualityQuestionActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.QualityTestActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.MySafetySelectTreeView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/18.
 */

public class SafetyCheckItemsActivity extends BaseActivity implements AdapterView.OnItemClickListener{
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.save_tv)
    HXTextView saveTv;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.structure_lv)
    ListView structureLv;
    @BindView(R.id.content_rl)
    RelativeLayout contentRl;

    private int type;//2施工安全检查 3日常安全检查
    private SafetyTreeFirstAdapter adapter;
    private List<SafetyBean> safetyBeanList, datas, leafs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safety);
        ButterKnife.bind(this);
        titleTv.setText("检查项目选择");
        if(safetyBeanList == null){
            safetyBeanList = new ArrayList<>();
        }
        getSafetyCheckItems("加载中…");
    }

    @OnClick({R.id.iv_back, R.id.save_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.save_tv:
                leafs = myTree.getSelectedDatas();
                if(leafs.size() > 0){
                    List<SafetyBean> selectedStructures = recursion(datas);
                    Intent intent = new Intent();
                    if(type == Constants.QUALITY){//1 质量检查界面 2日常安全检查 3质量问题 4安全问题
                        intent.setClass(SafetyCheckItemsActivity.this, QualityTestActivity.class);
                    }else if(type == Constants.SECURITY){//1 质量检查界面 2日常安全检查 3质量问题 4安全问题
                        intent.setClass(SafetyCheckItemsActivity.this, SecurityTestActivity.class);
                    }else if(type == Constants.DAILY_SECURITY){
                        intent.setClass(SafetyCheckItemsActivity.this, DailySecurityTestActivity.class);
                    }else if(type == Constants.QUALITY_QUESTION){
                        intent.setClass(SafetyCheckItemsActivity.this, QualityQuestionActivity.class);
                    }else if(type == Constants.SECURITY_QUESTION){
                        intent.setClass(SafetyCheckItemsActivity.this, SecurityQuestionActivity.class);
                    }
                    intent.putExtra("selectedDatas", (Serializable) selectedStructures);
                    intent.putExtra("leafDatas", (Serializable) leafs);
                    startActivity(intent);
                }else{
                    showToast("请至少选择一项");
                }
//                if(safetyBeanList.size() > 0){
//                    Intent intent = new Intent();
//                    intent.setClass(this, SafetyCheckResultsActivity.class);
//                    intent.putExtra("safetyBeanList", (Serializable) safetyBeanList);
//                    intent.putExtra("type", type);//2施工安全检查 3日常安全检查
//                    startActivity(intent);
//                }else{
//                    showToast("请至少选择一项");
//                }
                break;
        }
    }

    private List<SafetyBean> recursion(List<SafetyBean> list){
        List<SafetyBean> newDatas = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            try {
                SafetyBean s = list.get(i);
                SafetyBean cs = (SafetyBean) s.clone();
                List<SafetyBean> datas = recursion(s.getData());
                if(leafs.contains(cs)){
                    newDatas.add(cs);
                }else{
                    if(datas.size() > 0){
                        cs.setData(datas);
                        newDatas.add(cs);
                    }
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }
        return newDatas;
    }

    private void getSafetyCheckItems(String alert) {
        JSONObject jsonObject = new JSONObject();
        try {
            type = getIntent().getIntExtra("type", Constants.SECURITY);
            if(type == Constants.QUALITY){
                jsonObject.put("type", type + "");
            }else if(type == Constants.SECURITY){
                jsonObject.put("type", type + "");
            }else if(type == Constants.DAILY_SECURITY){
                jsonObject.put("type", type + "");
            }else if(type == Constants.QUALITY_QUESTION){
                jsonObject.put("type", Constants.QUALITY + "");
            }else if(type == Constants.SECURITY_QUESTION){
                jsonObject.put("type", Constants.DAILY_SECURITY + "");
            }
            jsonObject.put("constructionId", SPUtil.getString(this,"constructionid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/inspection/querySafetyCheckItems/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    SafetyRes safetyRes = gson.fromJson(json, SafetyRes.class);
                    if(null != safetyRes.getValue() && safetyRes.getValue().size()>0){
                        datas = safetyRes.getValue();
                        process();
                    }else{
                        showEmptyView(structureLv);
                    }

                }
            }

            @Override
            public void onFailure(String json) {
                showEmptyView(structureLv);
            }
        });
    }

//    private void process(){
//        adapter = new SafetyTreeFirstAdapter(datas, SafetyCheckItemsActivity.this);
//        structureLv.setAdapter(adapter);
//        structureLv.setOnItemClickListener(SafetyCheckItemsActivity.this);
//    }

    private MySafetySelectTreeView myTree;
    private void process(){
        myTree=new MySafetySelectTreeView(this);
        myTree.setDatas(datas);
        contentRl.addView(myTree);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SafetyBean safetyBean = datas.get(position);
        if(safetyBeanList.contains(safetyBean)){
            safetyBeanList.remove(safetyBean);
            safetyBean.setSelected(false);
        }else{
            safetyBeanList.add(safetyBean);
            safetyBean.setSelected(true);
        }
        adapter.notifyDataSetChanged();
    }
}
